package com.wubo.sec.model;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.wubo.sec.acl.RoleAcl;
import com.wubo.sec.acl.core.AclModel;
import com.wubo.sec.acl.core.GlobalAcl;
import com.wubo.sec.core.RoleData;
import com.wubo.sec.model.User;

/**
 * 角色
 * @author WuBo
 * @CreateDate 2010-7-25
 * @version 1.0.01
 */
@Entity
@Table(name="SEC_ROLE")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Role implements RoleData, Cloneable, AclModel {
	private static final long serialVersionUID = 5988331719017531600L;

	public final static String ADMIN_ROLE = "ADMIN_ROLE";
	
	public GlobalAcl getAcl() {
		return new RoleAcl(this);
	}
	
	@Id
	@Column(unique=true, nullable=true, length=32)
	private String code; //编码，主键
	
	@Column(unique=true, nullable=true, length=30)
	private String roleName; //角色名

	private Date createTime; //创建时间
	private String createUser; //创建用户
	private Date updateTime; //修改时间
	private String updateUser; //修改用户
	
	private String desn; //描述
	
	private boolean locked;
	
	@Column(name="owner_rel")
	private String owner;
	
	@ManyToMany(targetEntity=User.class, mappedBy="roles", fetch=FetchType.LAZY)
	private Set<User> users; //用户列表

	@ManyToMany(targetEntity=Authority.class, fetch=FetchType.LAZY)
	@JoinTable(
		name="SEC_ROLE_AUTH",
		joinColumns=@JoinColumn(name="ROLE_REL"),
		inverseJoinColumns=@JoinColumn(name="AUTH_REL")
	)
	private Set<Authority> authorities; //权限列表
	
	public Role(){
		
	}

	public Role(String code, String roleName) {
		super();
		this.code = code;
		this.roleName = roleName;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public String getUpdateUser() {
		return updateUser;
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}

	public String getDesn() {
		return desn;
	}

	public void setDesn(String desn) {
		this.desn = desn;
	}

	public boolean isLocked() {
		return locked;
	}

	public void setLocked(boolean locked) {
		this.locked = locked;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public Set<Authority> getAuthorities() {
		return authorities;
	}

	public void setAuthorities(Set<Authority> authorities) {
		this.authorities = authorities;
	}
	
	public boolean equals(Object obj){
		if(obj==null)return false;
		if(obj instanceof Role){
			return ((Role) obj).getCode().equals(this.getCode());
		}
		return false;
	}
	
	public Role clone(){
		try{
			return (Role) super.clone();
		}catch(CloneNotSupportedException e){
			return null;
		}
	}
	
}
